<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="params.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="params.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="params.cardStatus">
        <el-option v-for="item in cardStatusList" :key="item.value" :value="item.value" :label="item.name" />
      </el-select>
      <el-button type="primary" class="search-btn" @click="selFn">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardAdd')">添加月卡</el-button>
      <el-button @click="allDelFn">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="cardList" @selection-change="selectionChangeFn">
        <el-table-column type="selection" />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="status" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{row}">
            <el-button size="mini" type="text">续费</el-button>
            <el-button size="mini" type="text">查看</el-button>
            <el-button size="mini" type="text" @click="$router.push(`/cardAdd?id=${row.id}`)">编辑</el-button>
            <el-button size="mini" type="text" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-container">
      <el-pagination layout="total, prev, pager, next" :total="total" :page-size="params.pageSize" @current-change="currentChangeFn" />
    </div>
  </div>
</template>

<script>
import { getCardListAPI, delCardAPI, deleteCardsAPI } from '@/apis/card'
export default {
  data() {
    return {
      cardList: [],
      params: {
        page: 1,
        pageSize: 10,
        carNumber: null, // 车辆号码
        personName: null, // 车主姓名
        cardStatus: null
      },
      total: 0,
      cardStatusList: [
        {
          name: '全部',
          value: null
        },
        {
          name: '可用',
          value: 0
        },
        {
          name: '已过期',
          value: 1
        }
      ],
      selDelRowList: []
    }
  },
  mounted() {
    this.getCardList()
  },
  methods: {
    async getCardList() {
      const res = await getCardListAPI(this.params)

      // 作用：把数据转换成要展示的文字
      const statusKey = {
        0: '可用',
        1: '已过期'
      }
      res.data.rows.map(obj => {
        // obj.cardStatus 数字当做key去statusKey字典里换出对应的字符串文字
        obj.status = statusKey[obj.cardStatus]
      })
      console.log(res)
      this.cardList = res.data.rows
      this.total = res.data.total
      // console.log(this.cardList)
    },
    currentChangeFn(nowPage) {
      this.params.page = nowPage // 把当前用户点击的页码赋予到查询参数对象中
      this.getCardList()
    },
    selFn() {
      this.params.page = 1
      this.getCardList()
    },
    // 单个删除
    delFn(id) {
      this.$confirm('此操作将永久删除该月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI(id)
        this.getCardList()
        this.$message.success(
          res.msg
        )
      }).catch((error) => {
        console.log(error)
      })
    },
    // 多项删除
    allDelFn() {
      this.$confirm('此操作将永久删除选中的这些月卡, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        console.log(this.selDelRowList)
        const idsList = this.selDelRowList.map(obj => obj.id)
        const res = await deleteCardsAPI(idsList)
        this.$message.success(res.msg)
        this.getCardList()
      }).catch((error) => {
        console.log(error)
      })
    },
    // 表格前面多选改变后触发
    selectionChangeFn(list) {
      // list:选择的多行对应数据对象
      this.selDelRowList = list
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-main {
    width: 220px;
    margin-right: 10px;
  }

  .search-btn {
    margin-left: 20px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>
